#include "settings.h"
#include <QFile>
#include <QDebug>
#include <QStringList>
#include <iostream>

Settings* Settings::instance()
{
	static Settings c;
	return &c;
}
Settings::Settings()
{
	kA = 0;
	kB = 0;
	kC = 0;

	interval =0.01;
	size=400;
	translation=100;
	
	amplitude = 0;
	freq = 0;
	phase = 0;
	position=0;
	setSourcesCount(2);
	setSourcePosition(0,0,0);
	setSourcePosition(1,0,0);
	setAmplitude(0,0);
	setAmplitude(1,0);
	setFreq(0,0);
	setFreq(1,0);
	setPhase(0,0);
	setPhase(1,0);

	sLoadFromConfigFile("settings.ini");
}
void Settings::sLoadFromConfigFile(const char* name)
{
	QFile f(name);
	if(!f.exists())
		return;
	setSourcesCount(2);
	f.open(QIODevice::ReadOnly|QIODevice::Text);
	qDebug() << "Loading data from file";
	QString b = (QString)(f.readAll());
	b.remove(" ");
	int snum=0;
	QStringList s = b.split("\n");
	QStringList ss;
	QStringList value;
	for(int i=0;i<s.size();i++){
		ss = s[i].split("\r");
		for(int j=0;j<ss.size();j++){
			if(ss[j].size() < 1)
				continue;
			if(ss[j][0] == ';')
				continue;
			if(ss[j] == "[source]"){
				snum++;
				setSourcesCount(snum);
				setSourcePosition(snum-1,0,0);
				setFreq(snum-1,0);
				setAmplitude(snum-1,0);
				setPhase(snum-1,0);
			}
			value = ss[j].split("=");
			if(value[0] == "position"){
				QStringList tmp = value[1].split(":");
				setSourcePosition(snum-1,tmp[0].toInt(),tmp[1].toInt());
			}
			if(value[0] == "freq"){
				setFreq(snum-1,value[1].toDouble());
			}
			if(value[0] == "amplitude"){
				setAmplitude(snum-1,value[1].toDouble());
			}
			if(value[0] == "phase"){
				setPhase(snum-1,value[1].toDouble());
			}
		}
	}
	f.close();
}
const double& Settings::getAmplitude(const int& source) const
{
	return amplitude[source];
}
const double& Settings::getPhase(const int& source) const
{
	return phase[source];
}
const double& Settings::getFreq(const int& source) const
{
	return freq[source];
}
void Settings::setAmplitude(const int& source, const double& value)
{
	amplitude[source] = value;
}
void Settings::setFreq(const int& source, const double& value)
{
	freq[source] = value;
}
void Settings::setPhase(const int& source, const double& value)
{
	phase[source] = value;
}
const int& Settings::getSourcesCount() const
{
	return sources;
}
void Settings::setSourcesCount(const int& count)
{
	if(count < 2)
		return;
	if(sources==count)
		return;

	double *a = new double[count];
	double *p = new double[count];
	double *f = new double[count];
	int *pos = new int[count*2];
	int l =0;
	if(count > sources)
		l = sources;
	else
		l = count;
	
	for(int i=0;i<l;i++){
		a[i] = amplitude[i];
		p[i] = phase[i];
		f[i] = freq[i];
	}
	for(int i=0;i<l*2;i++){
		pos[i] = position[i];
	}
	if(amplitude)
		delete amplitude;
	if(phase)
		delete phase;
	if(freq)
		delete freq;
	if(position)
		delete position;

	position = pos;
	amplitude = a;
	phase = p;
	freq = f;
	sources = count;
}
void Settings::getSourcePosition(const int& source, int &x, int &y) const
{
	x=position[source*2];
	y=position[source*2+1];
}

Settings::~Settings()
{
	if(amplitude)
		delete amplitude;
	if(phase)
		delete phase;
	if(freq)
		delete freq;
}
const double& Settings::getInterval() const
{
	return interval;
}
const int &Settings::getSize() const
{
	return size;
}
const int &Settings::getTranslation() const
{
	return translation;
}
void Settings::setSourcePosition(const int& source, const int& x, const int& y)
{
	position[source*2]=x;
	position[source*2+1]=y;
}
const int& Settings::getKA() const
{
	return kA;
}
const int& Settings::getKB() const
{
	return kB;
}
const int& Settings::getKC() const
{
	return kC;

}

void Settings::setKA(const int& value)
{
	kA = value;
	emit onUpdateDegree();
}
void Settings::setKB(const int& value)
{
	kB = value;
	emit onUpdateDegree();
}
void Settings::setKC(const int& value)
{
	kC = value;
	emit onUpdateDegree();
}
void Settings::serInterval(const int& value)
{
	interval=value;
}
void Settings::setSize(const int& value)
{
	size=value;
}
void Settings::setTranslation(const int& value)
{
	translation=value;
}
